package com.app.hanoitips.travel;

import java.io.InputStream;
import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.app.hanoitips.travel.custlistview.XMLParser;
import com.app.hanoitips.travel.maps.MapUtils;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class DisplayDataTemp extends Activity implements View.OnClickListener{

	TextView dTitle,dContent;
	String name;
	String compName,sNo;
	Button phone,map,bBackward;
	ArrayList<String> arrAdds;
	private String sAddress;
	String url;
	static final String KEY_SHOP = "shop"; // parent node
	static final String KEY_NAME = "name";
	static final String KEY_DESC = "desc";
	static final String KEY_PHONE = "phone";
	static final String KEY_ADDRESS="address";
	String URL;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//overridePendingTransition(0,android.R.anim.slide_in_left);
		setContentView(R.layout.details_data);
		Intent intents=getIntent();
		Bundle extras=intents.getExtras();
		name=extras.getString("key");
		url=extras.getString("url");
		dTitle=(TextView) findViewById(R.id.tTitle);
		dTitle.setText(name);
		dContent=(TextView) findViewById(R.id.tContents);	
		dContent.setMovementMethod(new ScrollingMovementMethod());
		//Buttons
		bBackward=(Button) findViewById(R.id.btBackward);
		phone=(Button) findViewById(R.id.btPhone);
		map=(Button) findViewById(R.id.btMap);
		map.setOnClickListener(this);
		phone.setOnClickListener(this);
		bBackward.setOnClickListener(this);
		AssetManager getAssets=getAssets();
        InputStream input;
        try {
                input = getAssets.open(url+".xml");
     
                 int size = input.available();
                 byte[] buffer = new byte[size];
                 input.read(buffer);
                 input.close();
     
                 // byte buffer into a string
                 String text = new String(buffer);
                 
                 URL=text;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        displayContent();

	}
   private void displayContent(){
       arrAdds=new ArrayList<String>();  
       XMLParser parser = new XMLParser();
	   try {
       Document doc=parser.getDomElement(URL) ;
       NodeList nl = doc.getElementsByTagName(KEY_SHOP);
       // looping through all song nodes &lt;song&gt;
       for (int i = 0; i < nl.getLength(); i++) {
           // creating new HashMap
           
           Element e = (Element) nl.item(i);
           // adding each child node to HashMap key =&gt; value
            compName= parser.getValue(e, KEY_NAME);
 		if(name.equalsIgnoreCase(compName)){
			dContent.setText(parser.getValue(e, KEY_DESC));
		    sNo=parser.getValue(e, KEY_PHONE);
		    sAddress=parser.getValue(e, KEY_ADDRESS);
		    arrAdds=MapUtils.getAddresses(sAddress);
   		}
       }
	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
	}
   }
@Override
public void onClick(View v) {
	// TODO Auto-generated method stub
	switch(v.getId()){
	case R.id.btMap:
		if(name.equalsIgnoreCase("taxi")){
			Intent taxiIntent=new Intent("android.intent.action.MyLocation");
			startActivity(taxiIntent);
		}
		 Intent mIntent=new Intent("android.intent.action.ShowMap");
		 Bundle mBundle=new Bundle();
		 mBundle.putStringArrayList("key", arrAdds);
		 mIntent.putExtras(mBundle);
		 startActivity(mIntent);
		break;
	case R.id.btBackward:
		finish();
		break;
	case R.id.btPhone:
		Intent callIntent = new Intent(Intent.ACTION_CALL);
	    callIntent.setData(Uri.parse(sNo));
	    startActivity(callIntent);
		break;
	}
}
}
